var WxSearch = require('../../../../wxSearch/wxSearch.js')
var bqgUtil = require('../../../../utils/biqugeUtil.js');
var app = getApp()
var that;
var smoodList;
Page({
  data: {
    buttonClicked: false, //是否点击跳转
    tradeType: 'xiaoshuodaquan',
    moodList: [],
    isEmpty: true,
    loading: false,
    loadingMore: true,
    page: 1,
    pageSize:20,
    windowWidth:0,
    windowHeight:0
  },
  //选择要查询的活动类型
  choseTradeType: function (e) {
    wx.showLoading({ title: '加载中' });
    that.setData({ moodList: [], loadingMore: true, "wxSearchData.value":''});
    var tradeType = e.currentTarget.id;
    var key = "sort-" + tradeType + "-list";
    if (!this.data["'" + key +"'"]){
      var list = getSearchType(tradeType, 1, key);
    }else{
      that.setData({ moodList: this.data.key });
    }
    this.setData({
      tradeType: tradeType
    })
  },
  onLoad: function () {
    that = this;
    wx.showLoading({title: '加载中'});
    var key = "sort-" + that.data.tradeType + "-list";
    getSearchType(that.data.tradeType, 1, key);

    //初始化的时候渲染wxSearchdata
    var hot = ['圣域', '最强装逼', '电影'];
    WxSearch.init(that, 43, hot);
    WxSearch.initMindKeys(hot);
  },

  onShow: function () {
    that.setData({
      windowWidth:app.globalData.windowWidth,
      windowHeight:app.globalData.windowHeight
    });
    var molist = new Array();
  },

  bindNovelDetail: function(e){
    var url = e.currentTarget.dataset.href;
    wx.navigateTo({
      url: '/pages/my/novel/detail/detail?url=' + url,
    })
  },
  
  //js 实现模糊匹配查询
  findEach: function (e) {
    var that = this
    WxSearch.wxSearchAddHisKey(that);
    var strFind = that.data.wxSearchData.value;
    console.log("strFind=" + strFind);
    if (strFind == null || strFind == "" || typeof strFind == 'undefined') {
      wx.showToast({
        title: '输入为空',
        icon: 'loading',
      })
      setTimeout(function(){
        wx.hideToast();
      }, 300);
      return;
    }
    if (strFind != "") {
      WxSearch.updateHotMindKeys(that, strFind); //更新热门搜索和搜索记忆提示
      var nPos;
      var resultPost = [];
      that.setData({
        tradeType:'',
        moodList: []
      });
      getSearchResult(strFind);
    }
  },

  /** 页面滑动到底部 */
  handleLower: function (event) {
    console.log("loadingmore");
    if (!that.data.loadingMore) return;
    wx.showLoading({ title: '加载中' });
    var key = "sort-" + that.data.tradeType + "-list";
    getSearchType(that.data.tradeType, ++that.data.page, key);
  },
  //--------------------------------------------------------

  wxSearchInput: function (e) {
    var that = this
    WxSearch.wxSearchInput(e, that);
  },
  wxSerchFocus: function (e) {
    var that = this
    WxSearch.wxSearchFocus(e, that);
  },
  wxSearchBlur: function (e) {
    var that = this
    WxSearch.wxSearchBlur(e, that);
  },
  wxSearchKeyTap: function (e) {
    var that = this
    WxSearch.wxSearchKeyTap(e, that);
  },
  wxSearchDeleteKey: function (e) {
    var that = this
    WxSearch.wxSearchDeleteKey(e, that);
  },
  wxSearchDeleteAll: function (e) {
    var that = this;
    WxSearch.wxSearchDeleteAll(that);
  },
  wxSearchTap: function (e) {
    var that = this
    WxSearch.wxSearchHiddenPancel(that);
  }
})

function getSearchResult(q){
  var url = bqgUtil.getHost() + "home/search";
  console.log("begin request:" + url);
  wx.showLoading({ title: '加载中' });
  var data = { action: 'search', q: q };
  wx.request({
    url: url,
    method: 'POST',
    header: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: data,
    success: function (res) {
      console.log("成功获取远程数据：" + res.data);
      if (res.statusCode == 200){
        if (res.data.indexOf('没有搜到相关内容') != -1){
          wx.showToast({
            title: '没有搜到相关内容',
          })
          setTimeout(function(){
            wx.hideToast();
          }, 500);
          return;
        }
        if (res.data.indexOf("章节列表") == -1 && res.data.indexOf("最新") == -1) {
          //搜索到相似内容
          var list = bqgUtil.parseSearchResultList(res.data);
          that.setData({ moodList: list});
          if (that.data.moodList.length == 0) {
            that.setData({ pageSize: list.length });
          }
          wx.hideLoading();
          return;
        }
        wx.showLoading({
          title: '立刻为您跳转',
        })
        var nextUrl = '/pages/my/novel/detail/detail?url=' + url + "&data=" + JSON.stringify(data);
        console.log(nextUrl);
        wx.navigateTo({
          url: nextUrl,
        })
        wx.setData({ "wxSearchData.value": ''});
      }
    },
    error: function(e){
      console.log("请求失败:"+JSON.stringify(e));
    },
    complete: function () {
    }
  });
}
function getSearchType(type, page, key) {
  var url = bqgUtil.getHost() + type + "/page_" + page + ".html";
  console.log("begin request:" + url);
  wx.showLoading({ title: '加载中' });
  wx.request({
    url: url,
    header: { 'Content-Type': 'text/html' },
    success: function (res) {
      if (res.statusCode == 200) {
        var list = bqgUtil.parseSortList(res.data);
        that.setData({ moodList: that.data.moodList.concat(list) });
        if (that.data.moodList.length == 0) {
          that.setData({ pageSize: list.length });
        }
        if (that.data.moodList.length != 0) {
          if (list.length < that.data.pageSize) {
            that.setData({ loadingMore: false });
          }
        }
      }
    },
    error: function (e) {
      console.log("请求失败:" + JSON.stringify(e));
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}


